import Vue from "vue";
import VueRouter from "vue-router";

import Home from "../views/Home";
import Login from "../views/Login";
import Reg from "../views/Reg";
import Worth from "../views/Worth";
import Cart from "../views/Cart";
import Mine from "../views/Mine";
import Search from "../views/Search";
import Goods from "../views/Goods";
import Sort from "../views/Sort";
import Setting from "../views/Setting";
import Category from "../views/Category";
import Searchgoods from "../views/Searchgoods";
import OrdersItem from "../views/OrdersItem";
import Orders from "../views/Orders";
import Userinfo from "../views/Userinfo";
import Confirmorder from '../views/Confirmorder'
import Address from '../views/Address'
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};
Vue.use(VueRouter);

const routes = [{
        path: "/",
        redirect: "/home",
    },
    {
        path: "/logout",
        redirect: "/login?logout=true",
    },
    {
        name: "Home",
        path: "/home",
        component: Home,
    },
    {
        name: "Login",
        path: "/login",
        component: Login,
    },
    {
        name: "Reg",
        path: "/reg",
        component: Reg,
    },
    {
        name: "Worth",
        path: "/worth",
        component: Worth,
    },
    {
        name: "Cart",
        path: "/cart",
        component: Cart,
        meta: {
            requiresAuth: true,
        },
    },
    {
        name: "Mine",
        path: "/mine",
        component: Mine,
        meta: {
            requiresAuth: true,
        },
    },
    {
        name: "Sort",
        path: "/sort",
        component: Sort,
    },
    {
        name: "Search",
        path: "/search",
        component: Search,
    },
    {
        name: "Goods",
        path: "/goods/:id",
        component: Goods,
        meta: {
            requiresAuth: true,
        },
    },
    {
        name: "Setting",
        path: "/setting",
        component: Setting,
    },
    {
        name: "Category",
        path: "/category",
        component: Category,
    },
    {
        name: "Searchgoods",
        path: "/searchgoods",
        component: Searchgoods,
    },
    {
        name: "OrdersItem",
        path: "/ordersItem/:id",
        component: OrdersItem,
    },
    {
        name: "Orders",
        path: "/orders",
        component: Orders,
    },
    {
        name: "Userinfo",
        path: "/userinfo",
        component: Userinfo,
    },
    {
        name: 'Confirmorder',
        path: '/confirmorder',
        component: Confirmorder
    }, {
        name: 'Address',
        path: '/address',
        component: Address
    }
];
const router = new VueRouter({
    routes,
    // 保持滚动条位置
    scrollBehavior(to, from, savedPosition) {
        // savePosition: 当前页面切换出去时的滚动条位置
        // console.log('savedPosition=', savedPosition)
        // return 期望滚动到哪个的位置
        // return {x:0,y:100}
        // 异步滚动操作
        return new Promise((resolve) => {
            setTimeout(() => {
                resolve(
                    savedPosition || {
                        x: 0,
                        y: 0,
                    }
                );
            }, 500);
        });
    },
});
// router.beforeEach((to, from, next) => {
//     if (to.meta.requiresAuth) {
//         let userInfo = JSON.parse(localStorage.getItem('userinfo')) || {}
//             // 判断用户是否登录
//         if (userInfo.authorization) {
//             // 只要有token，不管是否有效，先放行
//             next()
//                 // 放行后，校验token有效性
//             router.app.$axios.get('/api/user/verify', {
//                 params: {
//                     username: userInfo.username
//                 },
//                 headers: {
//                     authorization: userInfo.authorization
//                 }
//             }).then(({ data }) => {
//                 if (data.code === 400) {
//                     localStorage.removeItem('user')
//                     router.push({
//                         path: '/login',
//                         query: {
//                             targetUrl: to.fullPath
//                         }
//                     })
//                 }
//             })
//         } else {
//             router.push({
//                 path: '/login',
//                 query: {
//                     targetUrl: to.fullPath
//                 }
//             })
//         }
//     } else {
//         next();
//     }
// });
export default router;